package tetris;


import java.awt.*;
import javax.swing.*;

import java.util.*;
import java.awt.event.*;
import javax.swing.event.*;


import java.awt.Toolkit;


public class JBrainTetris  extends JTetris{
	private  JCheckBox brainMode;   // see if use brain or not
	private  DefaultBrain  brain;   // to compute best move
	private  int  checkCount;       // to see if new piece added
	private  Brain.Move bestMove;   // best move for current piece
	private  JSlider adversary;     // adversary slider
	private  JLabel  adversaryState; 
	
	
	
	public JBrainTetris(int pixels) {
		super(pixels);
		brain = new DefaultBrain();
		checkCount = count;
		bestMove = new Brain.Move();
	}
	
	/*
	 * use adversary value to choose piece
	 */
	@Override
	public Piece pickNextPiece() {
		int luckyNumber = 1 + super.random.nextInt(99);
		if (luckyNumber >= adversary.getValue()) {
			adversaryState.setText("ok");
			return  super.pickNextPiece();
		}
		else {
			adversaryState.setText("*ok*");
			return computeWorstPiece();
		}
	}
	
	
	private Piece computeWorstPiece() {
		// uses the brain to do the work. 
		Brain.Move worstBestMove =  new Brain.Move();
		Brain.Move currBestMove  =  new Brain.Move();
		worstBestMove = brain.bestMove(board, super.pieces[0], HEIGHT, worstBestMove);
		Piece worstPiece = super.pieces[0];
		if (worstBestMove == null) return worstPiece;
		for (int i = 0; i < super.pieces.length; i++) {
			currBestMove = brain.bestMove(board, super.pieces[i], HEIGHT, currBestMove);
			if  (currBestMove == null) continue;
			if (currBestMove.score > worstBestMove.score) {
				worstBestMove  = currBestMove;
				worstPiece = super.pieces[i];
			}
		}
		return worstPiece;
	}

	/**
	 * using brain to plain intelligently
	 * 
	 */
	
	@Override
	public void tick(int verb) {
		if (brainMode.isSelected()) {
			board.undo();
			if (verb == DOWN) {
				if (checkCount != super.count) {
					bestMove = brain.bestMove(board, currentPiece, HEIGHT, bestMove);
					checkCount = super.count;
				}
				if (bestMove == null || bestMove.piece == null || currentPiece == null) {
					stopGame();
					return;
				}
				if (!currentPiece.equals(bestMove.piece))super.tick(ROTATE);
				if (currentX < bestMove.x) super.tick(RIGHT);
				if (currentX > bestMove.x) super.tick(LEFT);
			}
		}
		super.tick(verb);
	}
	
	/**
	 Creates the panel of UI controls -- controls wired
	 up to call methods on the JTetris. This code is very repetitive.
	*/
	@Override
	public JComponent createControlPanel() {
		JPanel panel = (JPanel)super.createControlPanel();
		panel.add(new JLabel("Brain:"));
		brainMode = new JCheckBox("Brain active");
		panel.add(brainMode);
		// add adversary slider
		JPanel little = new JPanel();
		little.add(new JLabel("Adversary:"));
		adversary = new JSlider(0, 100, 0); // min, max, current
		adversary.setPreferredSize(new Dimension(100, 15));
		little.add(adversary);
				
		panel.add(little);
		adversaryState = new JLabel("ok");
		panel.add(adversaryState);
		return panel;
	}
	
	/**
	 * @param args 
	 */
	public static void main(String[] args) {
		// Set GUI Look And Feel Boilerplate.
		// Do this incantation at the start of main() to tell Swing
		// to use the GUI LookAndFeel of the native platform. It's ok
		// to ignore the exception.
		try {  
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");     
        } catch (Exception ignored) { };  
        
		JBrainTetris tetris = new JBrainTetris(16);
		JFrame frame = JBrainTetris.createFrame(tetris);
		frame.setVisible(true);
	}

}
